package cn.jjxx.modules.portal.utils;

import cn.jjxx.core.utils.CacheUtils;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.sms.service.ISmsSendLogService;
import org.framework.superutil.j2se.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.MessageFormat;

@Component
public class SMSCodeUtil {

    @Autowired
    private ISmsSendLogService smsSendLogService;

    private static SMSCodeUtil smsCodeUtil;

    @PostConstruct
    public void init() {
        smsCodeUtil = this;
        smsCodeUtil.smsSendLogService = this.smsSendLogService;
    }

    /**
     * 发送短信验证码
     * 如果smsCodeEhcache.getSmsCode()为null，则表示在工具内自己生成验证码，且根据smsCodeEhcache.getMessageContent()格式化
     * 如果smsCodeEhcache.getSmsCode()不为null，则表示调用者已经设置好验证码及具体短信内容，这里直接发送
     * @param smsCodeEhcache 短信验证码ehcache对象
     * @return 验证码
     * @author mali
     * @date 2020-02-11
     */
    public static SMSCodeEhcache sendCode(SMSCodeEhcache smsCodeEhcache) {
        int i = 0;
        // 根据IP记录该IP从所有接口获取短信的次数，存储24小时  这个缓存是总计数器，不论什么接口调用，这里都+1
        Object countByIpObj = CacheUtils.get(MessageConfig.TAKE_SMS_CODE_COUNT, smsCodeEhcache.getIp());
        int countByIp = countByIpObj == null ? 0 : Integer.parseInt(countByIpObj.toString());
        countByIp++;
        // 如果需要计数
        if (smsCodeEhcache.getMaxCount() != null) {
            // 从缓存读取当前计数
            Object count = CacheUtils.get(smsCodeEhcache.getCountCacheName(), smsCodeEhcache.getCountCacheKey());
            i = count == null ? 0 : Integer.parseInt(count.toString());
            // 如果大于或等于最大次数，则返回
            if (smsCodeEhcache.getMaxCount() != null && i >= smsCodeEhcache.getMaxCount()) {
                smsCodeEhcache.fail("超过最大次数");
                return smsCodeEhcache;
            }
        }
        // 如果未生成验证码，则这里生成并格式化短信内容
        if (StringUtils.isBlank(smsCodeEhcache.getSmsCode())) {
            String randomNumber = RandomUtils.getRandomNumber(6);
            String msg = MessageFormat.format(smsCodeEhcache.getMessageContent(), randomNumber);
            // 发送短信写入日志
            smsCodeUtil.smsSendLogService.writeSmsLog(smsCodeEhcache.getPhone(), msg, StringUtils.randomUUID());
            smsCodeEhcache.setSmsCode(randomNumber);
            smsCodeEhcache.setMessageContent(msg);
        } else {
            // 发送短信写入日志
            smsCodeUtil.smsSendLogService.writeSmsLog(smsCodeEhcache.getPhone(), smsCodeEhcache.getMessageContent(), StringUtils.randomUUID());
        }

        // 如果需要计数
        if (smsCodeEhcache.getMaxCount() != null) {
            i++;
            CacheUtils.put(smsCodeEhcache.getCountCacheName(), smsCodeEhcache.getCountCacheKey(), i);
        }
        // 缓存验证码
        CacheUtils.put(smsCodeEhcache.getCodeCacheName(), smsCodeEhcache.getCodeCacheKey(), smsCodeEhcache.getSmsCode());

        CacheUtils.put(MessageConfig.TAKE_SMS_CODE_COUNT, smsCodeEhcache.getIp(), countByIp);
        smsCodeEhcache.success("发送成功");
        return smsCodeEhcache;
    }

    /**
     * 获取缓存中的短信验证码
     * @param smsCodeEhcache codeCacheName、codeCacheKey必须
     * @return 验证码
     * @author mali
     * @date 2020-02-11
     */
    public static SMSCodeEhcache getSMSCode(SMSCodeEhcache smsCodeEhcache) {
        String codeOfCache = (String) CacheUtils.get(smsCodeEhcache.getCodeCacheName(), smsCodeEhcache.getCodeCacheKey());
        smsCodeEhcache.setSmsCode(codeOfCache);
        return smsCodeEhcache;
    }

    public static SMSCodeEhcache getCount(SMSCodeEhcache smsCodeEhcache) {
        Object countObj = CacheUtils.get(smsCodeEhcache.getCountCacheName(), smsCodeEhcache.getCountCacheKey());
        Integer count = countObj == null ? 0 : Integer.parseInt(countObj.toString());
        smsCodeEhcache.setCount(count);
        return smsCodeEhcache;
    }
}
